package com.yunhe.merchants.controller;

import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.model.ResultObject;
import com.yunhe.common.model.mybatis.PageResult;
import com.yunhe.common.utils.ExcelUtils;
import com.yunhe.merchants.domain.MyProject;
import com.yunhe.merchants.model.FilterCondition;
import com.yunhe.merchants.model.ImportUpload;
import com.yunhe.merchants.model.IntentionProjectUpload;
import com.yunhe.merchants.service.IDataHistoryProjectStatusService;
import com.yunhe.merchants.service.IImportHistoryService;
import com.yunhe.merchants.service.IIntentionProjectService;
import com.yunhe.merchants.util.MyHeaderUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import com.yunhe.common.util.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author: DongWenYu
 * @Date: 2021/5/7 22:04
 */
@RestController
@RequestMapping("/import")
public class ImportHistoryController {

    @Autowired
    MyHeaderUtil headerUtil;

    @Autowired
    IImportHistoryService importHistoryService;

    @Autowired
    IDataHistoryProjectStatusService dataHistoryProjectStatusService;

    @ApiOperation("导入历史条件查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页展示的条数", dataType = "int", paramType = "query")
    })
    @GetMapping
    public ResponseEntity<ResultObject> selectByCondition(@ModelAttribute FilterCondition filterCondition,
                                                          @RequestParam("page") int page,
                                                          @RequestParam("size") int size) throws ArgumentErrorException {
        Long userId = headerUtil.getHeaderUser();
        if(userId == null ){
            throw new ArgumentErrorException("未获取到登录信息，请重新登录！");
        }
        PageResult result = importHistoryService.selectByFilterCondition(filterCondition,userId,page ,size);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    @ApiOperation("数据导出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "意向项目的id", required = true, dataType = "String", paramType = "query"),
    })
    @GetMapping("/export")
    public ResponseEntity<ResultObject> export(@RequestParam String ids,
                                               @ModelAttribute FilterCondition filterCondition,
                                               HttpServletResponse httpServletResponse) {
        List<MyProject> data = importHistoryService.export(ids , filterCondition);
        if (!CollectionUtil.isEmpty(data)){
            ExcelUtils.convertAndExport(data,httpServletResponse, ImportUpload.class);
        }
        return null;
    }

    @ApiOperation("查看导入详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "项目id", dataType = "long", paramType = "query")
    })
    @GetMapping("/detail")
    public ResponseEntity<ResultObject> selectById(@RequestParam("id") Long id){
        MyProject result = importHistoryService.selectById(id);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    @ApiOperation("查看导入历史的历史记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "项目id", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页数", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页展示的条数", dataType = "int", paramType = "query")
    })
    @GetMapping("/history")
    public ResponseEntity<ResultObject> selectHistory(@RequestParam("id") Long id,
                                                      @RequestParam("page") int page,
                                                      @RequestParam("size") int size){
        PageResult result = dataHistoryProjectStatusService.selectByProjectId(id,page ,size);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }
}
